package com.pur.zsms.supplier.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.cglib.beans.BeanCopier;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.accountmanage.vo.AccountVo;
import com.pur.zsms.check.vo.CheckVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.ExcelReader;
import com.pur.zsms.common.util.FileUtill;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.documentsmanage.vo.DomVo;
import com.pur.zsms.lockmanage.service.ILockTransService;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.MaterVoForCostSupplier;
import com.pur.zsms.matermanage.mater.vo.OrderDistributorMaterRealVo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.vo.ExcelVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterVo;
import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.pmsnmanage.user.web.UserControllor;
import com.pur.zsms.storemanage.po.StoreOutPo;
import com.pur.zsms.storemanage.service.IStoreService;
import com.pur.zsms.storemanage.vo.StoreOutVo;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.supplier.service.ISupplierPurchasesService;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.supplier.vo.ShopSupplierVo;
import com.pur.zsms.supplier.vo.SupplierDistributeVo;
import com.pur.zsms.supplier.vo.SupplierOrderCustVo;
import com.pur.zsms.supplier.vo.SupplierQualificationVo;
import com.pur.zsms.supplier.vo.SupplierVo;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.vo.CodeVo;
import com.pur.zsms.timecontrol.service.ITimeControlService;

/**
 * 供应商控制器
 * 
 * @author yuxin
 * 
 */
@Controller
@RequestMapping(value = "/supplierPurchases")
public class SupplierPurchasesController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SupplierPurchasesController.class);
	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "supplierPurchasesService")
	private ISupplierPurchasesService supplierPurchasesService;

	@Resource(name = "timeControlService")
	private ITimeControlService timeControlService;

	@Resource(name = "userService")
	private IUserService userService;

	@Resource(name = "lockTransService")
	private ILockTransService lockTransService;

	@Resource(name = "storeService")
	private IStoreService storeService;

	@RequestMapping(value = "/getMaterForPurchases.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getMaterForPurchases(HttpSession session, HttpServletRequest request, HttpServletResponse response, int materId, String date) {
		JSONObject json = new JSONObject();
		boolean flag = true;
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			int storeHouseId = supplierService.getSupStoreHouseIdByUserId(supId);

			MaterVo materVo = supplierPurchasesService.getMaterPoForPurchases(materId, storeHouseId, date, supId);

			json.put("materVo", materVo);

		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
			return json.toString();
		}
		json.put("success", true);
		return json.toString();
	}

	@RequestMapping(value = "/changeDistributorOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String changeDistributorOrder(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			OrderDistributorMaterRealVo orderDistributorMaterRealVo) {
		JSONObject json = new JSONObject();
		boolean flag = true;
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			int storeHouseId = supplierService.getSupStoreHouseIdByUserId(supId);

			List<OrderDistributorMaterRealVo> orderDistributorMaterRealVolist = supplierPurchasesService.changeDistributorOrder(
					orderDistributorMaterRealVo, storeHouseId, supId);
			if (orderDistributorMaterRealVolist != null && orderDistributorMaterRealVolist.size() > 0) {
				json.put("orderDistributorMaterRealVolist", orderDistributorMaterRealVolist);
				json.put("success", true);

			} else {
				json.put("success", false);
				json.put("msg", "客户没有此材料的订单！");
			}

		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
			return json.toString();
		}
		return json.toString();
	}
}
